package HLLib.http;

import java.util.Enumeration;
import java.util.Hashtable;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLObject;
import HLLib.base.HLByteList;
import HLLib.base.HLString;

public class HLAHttpObjectFile extends HLHttpObject {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_HTTP, HLClassManager_H.LIB_CLASS_HTTP_AHttpObjectFile);
    }

    public int GetInt(int index) {
        return super.GetInt(index);
    }

    public void SetInt(int index, int value) {
        super.SetInt(index, value);
    }

    public boolean GetBoolean(int index) {
        return super.GetBoolean(index);
    }

    public void SetBoolean(int index, boolean value) {
        super.SetBoolean(index, value);
    }

    public HLObject GetObject(int index) {
        return super.GetObject(index);
    }

    public void SetObject(int index, HLObject value) {
        super.SetObject(index, value);
    }

    public byte[] data1;

    public String filename;

    public String extense;

    public static final String BOUNDARY = "---------7d4a6d158c9";

    public void SetEvent(HLHttpListener e) {
        this.e = e;
    }

    public void Action(Object data) {
        if (e != null) {
            e.OnFinish(data);
        }
    }

    public String toString() {
        Hashtable table = CreateHashtable();
        Enumeration keys = table.keys();
        StringBuffer res = new StringBuffer("--").append(BOUNDARY).append("\r\n");
        int i = 0;
        while (keys.hasMoreElements()) {

            String key = (String) keys.nextElement();
            String value = (String) table.get(key);
            if (i != 0) {
                res.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
            }

            res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n").append("\r\n").append(value);
            i++;
        }
        return res.toString();
    }

    public String fileDes() {
        if (data1 != null) {
            StringBuffer sb = new StringBuffer();
            sb.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
            sb.append("Content-Disposition: form-data;name=\"attach\";filename=\"" + filename + "\"\r\n");

            sb.append("Content-Type: image/" + extense + "\r\n\r\n");

            return sb.toString();

        } else {
            return null;
        }

    }

    public String endString() {
        return "\r\n--" + BOUNDARY + "--\r\n";
    }

    public void SetFile(HLByteList bytes, HLString fileName, HLString extense) {
        this.extense = extense.string;
        this.filename = fileName.string;
        data1 = bytes.items;
    }

}
